<?php

namespace App\Http\Controllers\Auth;

use App\Facade\Auth;
use App\Http\Controllers\Controller;
use App\Service\WechatService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;

class WechatAuthController extends Controller
{

    public function oauth(Request $request)
    {
        $app = \EasyWeChat::officialAccount(); // 公众号
        $response = $app->oauth->scopes(['snsapi_userinfo'])
            ->redirect($request->fullUrl());
        return $response;
//        $user  = $app->oauth->setRequest($request)->user();
//        dd($user);
//        Log::info('ss', json_encode([$user,$data]));
//        // snsapi_base //snsapi_base 时不显示  //snsapi_userinfo
//        $response = $officialAccount->oauth->scopes(['snsapi_base'])
//            ->setRequest($request)
//            ->redirect();
    }

    public function serve()
    {
        $app = \EasyWeChat::officialAccount(); // 公众号
        $app->server->push(function ($message) {
            // $message['FromUserName'] // 用户的 openid
            // $message['MsgType'] // 消息类型：event, text....
            Log::info('push-message',$message);
            switch ($message['MsgType']) {
                case 'event':
                    Log::info('event',$message);
                    $mesg = (new WechatService())->EventMessage($message);
                    return $mesg;
                    break;
                case 'text':
                    Log::info('text',$message);
                    return '收到文字消息';
                    break;
                case 'image':
                    return '收到图片消息';
                    break;
                case 'voice':
                    return '收到语音消息';
                    break;
                case 'video':
                    return '收到视频消息';
                    break;
                case 'location':
                    return '收到坐标消息';
                    break;
                case 'link':
                    return '收到链接消息';
                    break;
                case 'file':
                    return '收到文件消息';
                // ... 其它消息
                default:
                    return '收到其它消息';
                    break;
            }
        });

// 在 laravel 中：
        $response = $app->server->serve();

// $response 为 `Symfony\Component\HttpFoundation\Response` 实例
// 对于需要直接输出响应的框架，或者原生 PHP 环境下
        $response->send();

// 而 laravel 中直接返回即可：
        return $response;
    }

    public function getUser()
    {
//        Log::
        Auth::updateUserInfo();
    }

}
